﻿/// <reference path="../_references.js" />
(function (e) {
    e.viewEngine = function (viewContainer) {
        this.currentView = null;
        this.currentViewName = "*";
        this.viewContainer = viewContainer;
    };
    e.viewEngine.prototype = {
        invokeView: function (state) {
            var that = this;
            var cv = this.currentView;

            //default view is home
            if (!state.view) state.view = "home";

            //check if view exists and if not throw exception
            Object.hasFunction(e.views, state.view, true, "View " + state.view + " not defined");

            if (state.view != this.currentViewName) {

                //check if current view is defined and unload it
                var loadNew = function () {
                    //load new view
                    that.currentView = new e.views[state.view](that.viewContainer);
                    that.currentViewName = state.view;
                    that.currentView._load.call(that.currentView, state);
                };
                if (cv) {
                    cv._unload.call(this.currentView);
                    this.viewContainer.html("");
                }
                loadNew();
            }
            else {
                //refresh view
                cv._refresh.call(this.currentView, state);
            }
        }
    };
} (this.eg = this.eg || {}));